package com.sholmes.android.scribbler;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.RemoteException;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ImageView;

@SuppressWarnings("unused")
public class AutoBehaviorActivity extends BasicRobotActivity {
	private static final String TAG = "------>AutoBehaviorActivity: ";
	private Button mMainMenuButton;
	
	private Button mSensorsButton;
	private TextView mStatusText;
	private ImageView mImageView;
	private ImageView mRedImageView;
		
	
	private boolean runningAutomaticBehavior = false;
	
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        setContentView(R.layout.auto_behavior);	        
	        init();
	    }
	 
	 private void init(){
		 
		 
		 mMainMenuButton = (Button)findViewById(R.id.mainMenuButton);
		 
		 mSensorsButton = (Button)findViewById(R.id.actSensors);
		 
		 mStatusText = (TextView)findViewById(R.id.statusText);
		 mImageView = (ImageView)findViewById(R.id.actImageView);
		 mRedImageView = (ImageView)findViewById(R.id.actRedImageView);
		 
		 
		 mMainMenuButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {								
				Log.d(TAG,"Calling finish.");
				finish();
			}
		});
		 

		 
		 mSensorsButton.setOnClickListener(new OnClickListener()
		 {
			 @Override
			 public void onClick(View v) 
			 {
				 takePhoto();
			 }
		 });
	 }
	 
	 private void takePhoto()
	 {
		try 
		{
			int[] imageData = service.getSensors();
			Bitmap b = Bitmap.createBitmap(256, 192, Bitmap.Config.ARGB_8888);
			Bitmap r = Bitmap.createBitmap(256, 192, Bitmap.Config.ARGB_8888);
			ImageThread thread = new ImageThread(imageData, b, mImageView, mStatusText, r, mRedImageView);
			thread.run();

		} 
		catch (RemoteException e) 
		{
			e.printStackTrace();
		}
	 }

	 /**
	  * Disabling the back button so the user doesn't leave the menu
	  * allowing them to stop the automatic behaviors.
	  * 
	  */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) { 
			return true;
		}else{
			return super.onKeyDown(keyCode, event);
		}
	}
	
	@Override
	void _connected(String device) {
		Log.d(TAG,"Connected");
		
	}

	@Override
	void _connecting(String device) {
		Log.d(TAG,"Connecting");
		
	}

	@Override
	void _disconnect() {
		Log.d(TAG,"Disconnected");
		
	}
	 
	  /** Called when the activity is about to be destroyed. */
	@Override
	protected void onDestroy(){
		Log.d(TAG,"onDestroy");
		
		super.onDestroy();
	}
	 
}